<template>
<!-- 出差添加 -->
    <div>
        <el-row style=" padding-left:30px; padding-top:20px;">
          <el-col :span="24">
            <div class="grid-content bg-purple-dark">
              <div class="kuang"></div>
              <span style=" padding-left:40px;" >加班申请</span>
              <div class="xian"></div>
              <div class="kuai">
                <span>开始时间</span>
                &nbsp;&nbsp;&nbsp;
                <el-date-picker
                    v-model="addLeave.startTime"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="请选择"
                    >
                </el-date-picker>&nbsp;&nbsp;
                <el-select  placeholder="请选择" v-model="addLeave.a" style="width:170px">
                <el-option label="上午" value="1"></el-option>
                <el-option label="下午" value="1"></el-option>
                </el-select>
                <br/> <br/> <br/>
                <span>结束时间</span>
                &nbsp;&nbsp;&nbsp;
                <el-date-picker
                    v-model="addLeave.endTime"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="请选择"
                    >
                </el-date-picker>&nbsp;&nbsp;
                <el-select  placeholder="请选择" v-model="addLeave.b" style="width:170px">
                <el-option label="上午" value="1"></el-option>
                <el-option label="下午" value="1"></el-option>
                </el-select>
                <br/> <br/> <br/>
                <span>加班时长</span>
                &nbsp;&nbsp;&nbsp;
                 <el-input placeholder="请输入内容" v-model="addLeave.duration" style="width:400px"></el-input>天
                  <br/> <br/> <br/>
                
                <span style=" position: relative;top:-70px">加班事由</span>
                &nbsp;&nbsp;&nbsp;
                 <el-input
                style="width:400px"
                type="textarea"
                :rows="4"
                placeholder="请输入内容"
                v-model="addLeave.matter">
                </el-input>
              </div>
              <br/><br/><br/><br/><br/>
              <div class="kuang"></div>
              <span style=" padding-left:40px;" >审批记录</span>
              <div class="xian"></div>
               <div class="kuai2">
                <div style="height: 300px;">
                 <el-timeline >
                    <el-timeline-item color="#409EFF">发起</el-timeline-item>
                     <el-timeline-item color="#67C23A">
                        审批人 &nbsp;&nbsp;
                        <el-tag size="small" effect="plain" >依次审批</el-tag>
                        <br/><br/>
                        <!-- <el-select v-model="addLeave.approver" multiple placeholder="请选择" style="width:100px">
                            <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.nickName"
                            :value="item.id">
                            </el-option>
                        </el-select> -->
                        <div  class="avatar-uploader">
                                        <!-- 选中负责人后展示start -->
                                        <div style="display: inline;" v-if="unSelectedUsers!=''">
                                            <div style="display: inline;" v-for="selectedUser,index in unSelectedUsers" :key="index">
                                                <img style="vertical-align: middle;"   :src="selectedUser.icon" class="avatar" @click="getUnSelectIdPage()">
                                                <i style="position: relative;bottom: 13px;right: 12px;border-radius: 50%;background-color: #c0c4cc;color: white;" @click="unDeUser(index)" class="el-icon-close"></i>
                                            </div>
                                        </div>
                                        
                                        <!-- 选中负责人后展示end -->
                                        <!-- 负责人未选中展示 -->
                                        <i v-else @click="getUnSelectIdPage()" class="el-icon-plus avatar-uploader-icon"></i>
                                    </div>
                                     <!-- 无需考勤 -->
                                    <el-dialog
                                        v-loading="loading"
                                        element-loading-text="拼命加载中"
                                        element-loading-spinner="el-icon-loading"
                                        element-loading-background="rgba(0, 0, 0, 0.8)"
                                        :visible.sync="unSelectIdPage"
                                        width="50%"
                                        :show-close="false">
                                        <div style="height: 200px;">
                                            <el-breadcrumb separator-class="el-icon-arrow-right">
                                                <!-- 步骤条start -->
                                                <el-breadcrumb-item  v-for="(value,index) in headNavs" :key="index+'E'" @click="upNav(index)">
                                                    <el-button v-if="persons.id==undefined" size="mini" style="border: 0px;" @click="upNav(index)">{{ value }}</el-button>
                                                </el-breadcrumb-item>
                                                <!-- 步骤条end -->
                                                <br>
                                                <br>
                                                <br>
                                                <!-- 展示右边所选中的用户start -->
                                                <div style="float: right;margin-top: -70px;margin-right:30%;color: black;">
                                                    已选择<span v-if="unSelectedUsers!=''">{{ unSelectedUsers.length }}</span><span v-else>0</span>位联系人
                                                    <br>
                                                    <br>
                                                    <br>
                                                </div>
                                                <!-- 展示选中的用户 -->
                                                <div v-for="selectedUser,index in unSelectedUsers" :key="index">
                                                    <div class="architecture" style="height: 50px;position: relative;top: 12px;left: 430px;">
                                                        <img style="width: 30px;height: 30px;position: relative;top: 10px;" :src="selectedUser.icon"/>
                                                        <span style="margin-left: 17%;color: black;color: black;font-size: 10px;display: block;margin-top: -20px;">
                                                            {{ selectedUser.nickName }}<br><br>
                                                            {{ selectedUser.tposition.deptName }}
                                                            <span style="float: right;margin-top: -10px;margin-right: 20px;cursor: pointer;" @click="unDeUser(index)">X</span>
                                                        </span>
                                                    </div>
                                                </div>
                                                
                                                <!-- 展示右边所选中的用户end -->
                                                <!-- 展示左边信息start -->
                                                <!-- 展示部门和人数start -->
                                                <div style="position: absolute; top: 100px;">
                                                    <div class="architecture" style="line-height: 39px;" v-for="(value,perIndex) in persons" :key="perIndex" @click="selectArchitecture(value.deptName)">
                                                        <div class="log">
                                                            <img src="https://cdn7.axureshop.com/demo2023/2236859/images/%E8%AF%B7%E5%81%87/u2303.svg" />
                                                        </div>
                                                        <span style="margin-left: 5%;color: black;">
                                                            {{ value.deptName }}
                                                        </span>
                                                        &nbsp;&nbsp;&nbsp;({{ value.deptCount }})
                                                        <span style="float: right;margin-right: 20px;">
                                                            ＞
                                                        </span>
                                                    </div>
                                                    <!-- 展示部门和人数end -->
                                                    <br>
                                                    <!-- 展示当前所选部门的所有用户start -->
                                                    <div v-if="users.length!=0">
                                                        <el-checkbox-group v-model="unSelectIds"  @change="unSelectUser()">
                                                            <div class="architecture" style="height: 50px;"  v-for="(myUser,userIndex) in users" :key="userIndex"   >
                                                                <el-checkbox style="width: 80%;height: 100%;" :label="myUser.id">
                                                                    &nbsp;&nbsp;&nbsp;
                                                                    <img style="width: 30px;height: 30px;position: relative;top: 10px;" :src="myUser.icon"/>
                                                                    <span style="margin-left: 5%;color: black;font-size: 10px;float: left;position: relative;top: 12px;left: 110px;">
                                                                        {{ myUser.nickName }}<br>
                                                                        {{ myUser.tposition.deptName }}
                                                                    </span>
                                                                    <span style="margin-top: 25px;margin-left: 120px;">
                                                                        ＞
                                                                    </span>
                                                                </el-checkbox>
                                                            </div>
                                                        </el-checkbox-group>
                                                    </div>
                                                    <!-- 展示当前所选部门的所有用户start -->
                                                    <!-- 展示左边信息end -->

                                                </div>

                                                                                            
                                            </el-breadcrumb>

                                            <!-- 竖直分割线 -->
                                            <div class="myHr"></div>
                                        </div>
                                        <span slot="footer" class="dialog-footer">
                                            <el-button @click="unSelectIdPage = false">取 消</el-button>
                                            <el-button type="primary" @click="unSelectIdPage = false">确 定</el-button>

                                        </span>

                                    </el-dialog>
                     </el-timeline-item>
                     <el-timeline-item color="#E6A23C">抄送人
                         <br/><br/>
                        <!-- <el-select v-model="addLeave.carbon" placeholder="请选择" style="width:100px">
                        <el-option
                        v-for="item in options"
                        :key="item.id"
                        :label="item.nickName"
                        :value="item.id">
                        </el-option>
                        </el-select> -->
                        <!-- 选择抄送人start -->

                                    <div class="avatar-uploader">

                                        <!-- 选中负责人后展示start -->
                                        <div v-if="selectedUser != ''">
                                            <img :src="selectedUser.icon" class="avatar" @click="headPage = true">
                                            <span style="position: relative;top: -17px;left: 20px;">
                                                {{selectedUser.nickName }}
                                                <span style="margin-left: 26px;cursor: pointer;"
                                                    @click="deUser">X</span>
                                            </span>
                                        </div>
                                        <!-- 选中抄送人后展示end -->
                                        <!-- 负责人未选中展示 -->
                                        <i v-else @click="headPage = true" class="el-icon-plus avatar-uploader-icon"></i>
                                    </div>


                                    <el-dialog v-loading="loading" element-loading-text="拼命加载中"
                                        element-loading-spinner="el-icon-loading"
                                        element-loading-background="rgba(0, 0, 0, 0.8)" :visible.sync="headPage"
                                        width="50%" :show-close="false">
                                        <el-breadcrumb separator-class="el-icon-arrow-right">
                                            <!-- 步骤条start -->
                                            <el-breadcrumb-item v-for="(value, index) in headNavs" :key="index"
                                                @click="upNav(index)">
                                                <el-button v-if="persons.id == undefined" size="mini" style="border: 0px;"
                                                    @click="upNav(index)">{{ value }}</el-button>
                                            </el-breadcrumb-item>
                                            <!-- 步骤条end -->
                                            <br>
                                            <br>
                                            <br>
                                            <!-- 展示右边所选中的用户start -->
                                            <div style="float: right;margin-top: -70px;margin-right:30%;color: black;">
                                                已选择<span v-if="selectedUser != ''">1</span><span v-else>0</span>位联系人
                                                <br>
                                                <br>
                                                <br>
                                            </div>
                                            <!-- 展示选中的用户 -->
                                            <div v-if="selectedUser != ''" class="architecture"
                                                style="height: 50px;float: right;    position: relative;top: 12px;right: 50px;">
                                                <img style="width: 30px;height: 30px;position: relative;top: 10px;"
                                                    :src="selectedUser.icon" />
                                                <span
                                                    style="margin-left: 17%;color: black;color: black;font-size: 10px;display: block;margin-top: -20px;">
                                                    {{ selectedUser.nickName }}<br><br>
                                                    {{ selectedUser.tposition.deptName }}
                                                    <span
                                                        style="float: right;margin-top: -10px;margin-right: 20px;cursor: pointer;"
                                                        @click="deUser">X</span>
                                                </span>

                                            </div>
                                            <!-- 展示右边所选中的用户end -->
                                            <!-- 展示左边信息start -->
                                            <!-- 展示部门和人数start -->
                                            <div class="architecture" style="line-height: 39px;"
                                                v-for="(value, perIndex) in persons" :key="perIndex"
                                                @click="selectArchitecture(value.deptName)">
                                                <div class="log">
                                                    <img
                                                        src="https://cdn7.axureshop.com/demo2023/2236859/images/%E8%AF%B7%E5%81%87/u2303.svg" />
                                                </div>
                                                <span style="margin-left: 5%;color: black;">
                                                    {{ value.deptName }}
                                                </span>
                                                &nbsp;&nbsp;&nbsp;({{ value.deptCount }})
                                                <span style="float: right;margin-right: 20px;">
                                                    ＞
                                                </span>
                                            </div>
                                            <!-- 展示部门和人数end -->
                                            <br>
                                            <!-- 展示当前所选部门的所有用户start -->
                                            <div v-if="users.length != 0">
                                                <div :class="[selectArchitectureCss != userIndex ? 'architecture' : '', selectArchitectureCss == userIndex ? 'architectureActe' : '']"
                                                    style="height: 50px;" v-for="(myUser, userIndex) in users"
                                                    :key="userIndex" @click="selectUser(userIndex)">
                                                    <el-radio style="width: 80%;height: 100%;" v-model="selectId"
                                                        :label="userIndex">
                                                        &nbsp;&nbsp;&nbsp;
                                                        <img style="width: 30px;height: 30px;position: relative;top: 10px;"
                                                            :src="myUser.icon" />
                                                        <span
                                                            style="margin-left: 5%;color: black;font-size: 10px;float: left;position: relative;top: 12px;left: 110px;">
                                                            {{ myUser.nickName }}<br><br>
                                                            {{ myUser.tposition.deptName }}
                                                        </span>

                                                    </el-radio>
                                                    <span>
                                                        ＞
                                                    </span>
                                                </div>
                                            </div>
                                            <!-- 展示当前所选部门的所有用户start -->
                                            <!-- 展示左边信息end -->

                                        </el-breadcrumb>

                                        <!-- 竖直分割线 -->
                                        <div class="myHr"></div>


                                        <span slot="footer" class="dialog-footer">
                                            <el-button @click="headPage = false">取 消</el-button>
                                            <el-button type="primary" @click="headPage = false">确 定</el-button>
                                        </span>
                                    </el-dialog>
                                    <!-- 选择负责人end -->
                     </el-timeline-item>
                </el-timeline>
                <el-row style=" position: relative;left:900px">
                <el-button @click="handleEdit">取消</el-button>
                <el-button type="primary" @click="onSubmit">提交</el-button>
                </el-row>
                </div>
               </div>
            </div>
        </el-col>
        </el-row>
    </div>
  
</template>

<script>
import axios from 'axios'
export default {
    data(){
        return{
           addLeave:{
            title:"", //当前登录人名字
            leaveType:"",
            startTime:"",
            endTime:"",
            duration:"",
            matter:"",
            approver:"",
            carbon:"",
            status:"1",  // 状态默认为1 1为申请中 
            sponsorId:""  //发起人ID  当前登录人

           },
            options: [],
          // 获取抄送人start
            headPage: false,//开启选择抄送人页面
            headNavs: ["组织架构"],//步骤条
            persons: [],//部门和部门人数
            users: [],//所选部门的人员
            loading: false,//加载开启和关闭
            selectArchitectureCss: -1,//选中指定行后开启指定的css
            selectId: -1,//单选框
            selectedUser: "",//选中的用户

             // 无需考勤
            unSelectIdPage:false,//开启选择无需考勤页面
            unSelectIds:[],//多选无需考勤
            unSelectedUsers:[],//无需考勤选中的用户
            //  获取抄送人end
          
        } 
},
    created(){
      this.getUserList();
      this.getDept();//获取部门
    },
   methods: {
    //id传一个当前登录人的id
    getUserList() {
       var a=JSON.parse(localStorage.getItem("user"))
            axios.get("demo/findTUserList?id="+a.id)
                .then((res) => {
                    console.log(res);
                     this.options = res.data;
                })
    },
    //添加审批
    onSubmit(){
       var a=JSON.parse(localStorage.getItem("user"))
        this.addLeave.sponsorId=a.id
         this.addLeave.title=a.nickName+"的加班申请"
      // var approver="";
      //  for (const value of this.addLeave.approver) {
      //   approver==""?approver+=value:approver+=","+value
      // }
      // this.addLeave.approver=approver
      axios
      .post("auditCenter/insertTAuditCenter", this.addLeave)
      .then((res) => {
        //待定
        console.log(res.data);
        if (res.data.code == 200) {
          this.$message({
            message: "恭喜你，" + res.data.msg,
            type: "success",
          });
        } else {
          this.$message.error("错了哦，" + res.data.msg);
        }
      });
      
     
     
    },
    //返回上一页
    handleEdit(){
      this.$router.push("/LaunchApplication"); //返回
     
    },
    // 负责人start
        // 更新步骤条
        upNav(index) {
            this.headNavs = this.headNavs.slice(0, index + 1);
            if ("组织架构" == this.headNavs) {
                this.loading = true;
                this.users = [];
                this.getDept();
            }
        },
        // 获取部门及部门人数
        getDept() {
            this.selectId = -1;
            this.deUser();
            axios.post("/ReplacementCard/findDept")
                .then((res) => {
                    this.persons = res.data.rows;
                    this.loading = false;
                })
        },
        // 获取所选择部门的用户信息
        selectArchitecture(value) {
            this.loading = true;
            this.persons = [];
            this.headNavs.push(value);
            var data = new FormData();
            data.append("name", value)
            axios.post("/ReplacementCard/findUserAndDept", data)
                .then((res) => {
                    this.users = res.data.rows;
                    if (this.users != []) {
                        this.loading = false;
                    }
                })

        },


        // 选中用户
        selectUser(index) {
            this.selectArchitectureCss = index;
            this.selectedUser = this.users[index];
            this.addLeave.carbon = this.selectedUser.id;
        },
        // 删除用户
        deUser() {
            this.selectId = -1;
            this.selectedUser = "";
            this.selectArchitectureCss = -1;
            this.addLeave.carbon = "";
        },
        // 负责人end

        // 无需考勤用户
        unSelectUser(){
            if(this.unSelectedUsers.length>=3){
                this.unSelectedUsers = [];
                this.unSelectIds = [];
                this.$message({
                    type:"warning",
                    message:"最多只能选择三个"
                })
            }else{
                this.addLeave.approver = this.unSelectIds.toString();
                for (const val of this.users) {
                    for (const id of this.unSelectIds) {
                        if(val.id==id){
                            if(this.unSelectedUsers.length==0){
                                this.unSelectedUsers.push(val);
                            }else{
                                let count = 0;
                                for (let i in this.unSelectedUsers) {
                                    if(this.unSelectedUsers[i].id==id){
                                        count++;
                                    }
                                    if(i==this.unSelectedUsers.length-1 && count==0){
                                        this.unSelectedUsers.push(val);
                                    }
                                }
                            }
                        }
                    }
                }
            }
           
            
        },
        // 删除
        unDeUser(index){
            this.unSelectIds.splice(index,1);
            
            let array = this.addLeave.approver.split(",");
            for (let val in array) {
                if(array[val]==this.unSelectedUsers[index].id){
                    array.splice(val,1);
                }
            }
            this.addLeave.approver = array.toString();
            this.unSelectedUsers.splice(index,1);

        },
        // 获取部门信息
        getUnSelectIdPage(){
            this.users = [];
            this.unSelectIdPage = true;
            this.headNavs = ["组织架构"];
            this.getDept();
        },

        
        // 考勤人员end
   }
}
</script>

<style scoped>
    .kuai{
         width:1000px;
        height:400px;
        /* border: 1px red solid; */
        position: relative;
        top: 30px;
        left: 55px;
    }
    .kuai2{
         width:1000px;
        height:300px;
        /* border: 1px red solid; */
        position: relative;
        top: 30px;
        left: 55px;
    }
    .kuang{
        width: 4.5px;
        height: 25px;
        background-color: #409EFF;
        margin-left: 30px;
        position: relative;
        top: 23px;
    }
    .xian{
        width: 1200px;
        height: 2px;
        background-color: #EBEEF5;
        margin-left: 40px;
        margin-top: 20px;
       
    }
 .el-row {
    margin-bottom: 20px;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: white;
    height: 1200px;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
   /* 获取负责人start */
    .architecture{
        width: 271px;
        height: 39px;
        padding-left: 20px;
        border-radius: 5px;
    }
    .architecture:hover{
        background-color: #f8f8f8;
    }
    .architectureActe{
        width: 271px;
        height: 39px;
        line-height: 39px;
        padding-left: 20px;
        border-radius: 5px;
        background-color: rgba(231, 244, 255, 1);;
    }
    /* 竖着的水平线 */
    .myHr{
        border-left: 2px solid#989DAA;;
        height:100%;
        left: 48%;
        top: 0px;
        position: absolute;
    }

    .log{
        width: 27px;
        height: 27px;
        background-color: rgba(158, 208, 255, 1);
        float: left;
        position: relative;
        top: 15%;
        border-radius: 50%;
    }
    .log img{
        width: 13px;
        height: 13px;
        position: relative;
        bottom: 15%;
        left: 25%;
    }
    .avatar-uploader-icon[data-v-626fa26d]{
        border: 1px solid rgba(198, 201, 208, 1);
        background-color: rgba(248, 248, 248, 1);
        border-radius: 50%;
    }
    .avatar-uploader-icon {
        font-size: 20px;
        color: #8c939d;
        width: 50px;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
    /* 获取负责人end */
</style>